//
// Created by THE13 on 2024/2/6.
//

#include "list_view.h"

/*--------------------private function--------------------------------------*/
static void PageNameDisplay(menu_t* page);//1.page name display
static void ItemNameDisplay(menu_t* item);//2.item name display
static void ItemIndicateDisplay(menu_t* page);//3.item indicate display

/*--------------------config view--------------------------------------*/
#define PAGE_NAME_FONT Font_JetBrainsMonoExtrabold2034  //set font
#define PAGE_NAME_FONT_OFFSET_BG 10 //font offset the background,and config the bg : PAGE_NAME_FONT_OFFSET_BG*2+font.height
#define ITEM_FONT_1 Font_JetBrainsMono1224

/*-------------------- list view--------------------------------------*/
void list_view(menu_t* page)
{
    menu_t* pr = page->next;
    menu_t* p = page->next;

    /*1.item name display*/
    for(;pr!= nullptr;pr=pr->same_next)
    {
        ItemNameDisplay(pr);
    }


    /*2.item indicate display*/
    ItemIndicateDisplay(page);

    /*3.page name display*/
    PageNameDisplay(page);

    for(;p!=nullptr;p=p->same_next)
    {
        if(p->itemtype == ITEM_TEXT)
        {
            if(p->param2 == 1)
            {
                float sys_out;
                float aligh;
                sys_out = SysUpdate(&ProgressBar_Ani, 0.022f,100, 0);
                aligh = 0+(240 - sys_out)/2;
                SDL2.setPencilMode(NORMAL,BLUE,WHITE);
                SDL2.drawRBox(0+((240-200)/2)-2, aligh-2,200+4,sys_out+4,5);
                SDL2.setPencilMode(NORMAL,WHITE,BLACK);
                SDL2.drawRBox(0+((240-200)/2), aligh,200,sys_out,5);
                SDL2.setPencilMode(NORMAL,BLACK,WHITE);
                SDL2.drawString(0+((240-200)/2)+5, aligh+5, "[FJJ]",Font_JetBrainsMono816);
            }
            if(p->param2 == 0)
            {
                //提示框的动画
                SysInit(&ProgressBar_Ani ,  2.0f, 0.5f, 1.00f, 40.0f);
            }
        }
    }//end for

}


/*1. Item name display************************************************/
static void ItemNameDisplay(menu_t* item)
{
    float sys_out ;
    float sys_tar ;

    /*1.item name*/
    sys_tar = 60+(item->id)*(ITEM_FONT_1.height) ;//选项的位置
    item->pos_y = sys_tar;
    if(item->last->state == onViewWillDisappear)sys_tar=240;//选项的消失位置
    sys_out = SysUpdate(item->ani, 0.3,sys_tar , 0);
    SDL2.setPencilMode(NORMAL,BLACK,WHITE);
    SDL2.drawString(item->pos_x,sys_out ,item->name,ITEM_FONT_1);

    /*2.item division line*/
    SDL2.setPencilMode(NORMAL,BLACK,WHITE);
    SDL2.drawHLine(240-6, item->pos_y, 6);
    SDL2.drawHLine(240-6, item->pos_y+1, 6);

    SDL2.drawHLine(240-6, item->pos_y+ITEM_FONT_1.height, 6);
    SDL2.drawHLine(240-6, item->pos_y+ITEM_FONT_1.height+1, 6);

    /*3.option customization graphics*/
    switch(item->itemtype)
    {
        case ITEM_SWITCH://开关选项
            SDL2.setPencilMode(NORMAL,BLACK,WHITE);
            SDL2.drawString(item->pos_x-15,sys_out,"-",ITEM_FONT_1);
            break;
        case ITEM_CHECKBOX:
            SDL2.setPencilMode(NORMAL,BLACK,WHITE);
            SDL2.drawString(item->pos_x-15,sys_out,"-",ITEM_FONT_1);
            //RFame
            SDL2.setPencilMode(NORMAL,BLACK,WHITE);
            SDL2.drawRBox(234-10-24, sys_out,24,24,5);
            SDL2.setPencilMode(NORMAL,WHITE,WHITE);
            SDL2.drawRBox(234-10-24+2, sys_out+2,20,20,5);

            //Rbox
            if(item->param2 == 0){SDL2.setPencilMode(NORMAL,WHITE,WHITE);}
            if(item->param2 == 1){SDL2.setPencilMode(NORMAL,BLUE,WHITE);}
            SDL2.drawRBox(234-10-24+2, sys_out+2,20,20,5);
            break;
        case ITEM_TEXT:
            SDL2.setPencilMode(NORMAL,BLACK,WHITE);
            SDL2.drawString(item->pos_x-15,sys_out,"+",ITEM_FONT_1);
            break;
        case ITEM_PROGRESS_BAR:
            SDL2.setPencilMode(NORMAL,BLACK,WHITE);
            SDL2.drawString(item->pos_x-15,sys_out,"-",ITEM_FONT_1);
            break;
        case ITEM_CALL_PAGE:
            SDL2.setPencilMode(NORMAL,BLACK,WHITE);
            SDL2.drawString(item->pos_x-15,sys_out,"+",ITEM_FONT_1);
            break;
        case ITEM_CALL_FUNCTION:
            break;
        default:break;
    }
}



/*2. Item indicate display************************************************/
static void ItemIndicateDisplay(menu_t* page)
{
    //1.get the string width
    uint8_t width = (uint8_t) (strlen(sys_info.now->name)) * ITEM_FONT_1.width + 10;
    //2.sys animation clac
    float sys_tar;
    sys_tar = sys_info.now->pos_y;//页面列表指示器显示的目标位置
    if(page->state == onViewWillDisappear)sys_tar=260;//面列表指示器消失的目标位置
    float sys_posYout = SysUpdate(&Rbox_posYani , 0.022f,sys_tar , 0);//动画计算输出值
    float sys_shapeOut = SysUpdate(&Rbox_shapAni , 0.022f,width , 0);//动画计算输出值
    //3.draw picture
    // - 文字框框
    SDL2.setPencilMode(TRA,BLACK,WHITE);
    SDL2.drawRBox(20-5, sys_posYout,sys_shapeOut,ITEM_FONT_1.height,1);//move indicate1

    // - 蓝色框框
    SDL2.setPencilMode(NORMAL,BLUE,WHITE);
    SDL2.drawRFrame(20-5, sys_posYout,sys_shapeOut,ITEM_FONT_1.height,1);
    SDL2.drawRFrame(20-6, sys_posYout-1,sys_shapeOut+2,ITEM_FONT_1.height+2,3);
    // - 指示条
    SDL2.setPencilMode(NORMAL,BLACK,WHITE);
    SDL2.drawBox(240-6, sys_posYout,6,25);//move indicate2
}


/*3. page name display************************************************/
static void PageNameDisplay(menu_t* page)
{
    uint8_t font_height = PAGE_NAME_FONT.height;
    uint8_t blackground_width  = 240;
    uint8_t font_offset_bg = PAGE_NAME_FONT_OFFSET_BG;

    /*1.check the state*/
    float sys_tar = page->pos_x;
    if(page->state == onViewWillDisappear)sys_tar=0;
    float sys_out = SysUpdate(page->ani, 0.03,sys_tar , 0);

    /*2.draw the background*/
    SDL2.setPencilMode(NORMAL,WHITE,WHITE);
    SDL2.drawBox(0,0,blackground_width,font_offset_bg*2+font_height);//fill blackground color
    SDL2.setPencilMode(NORMAL,BLACK,WHITE);
    SDL2.drawHLine(0,font_offset_bg*2+font_height,blackground_width);//draw the line1
    SDL2.drawHLine(0,font_offset_bg*2+font_height-1,blackground_width);//draw the line2
    /*2.draw the font*/
    SDL2.setPencilMode(NORMAL,BLACK,WHITE);
    SDL2.drawString(sys_out,font_offset_bg,page->name,PAGE_NAME_FONT);
}